<?php

namespace App\Http\Controllers\Home;

use App\Models\HotSearchs;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class SearchController extends Controller
{
    /**
     * 热门搜索
     * @param url home.search.hotSearchApi
     * @return bool
     */
    public function hotSearchApi() {
        try{
            $lists = HotSearchs::where('status','<>',-1)->orderBy("count", "desc")->limit(10)->get(['content','count','type']);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }
    /**
     * 搜索
     * @param url home.search.searchApi
     * @param keyword 搜索词
     * @param cid 分类
     * @param page 页码
     * @param sid 排序
     * @param orderby 排序方式
     * @param type 类型：1供应商 2活动 3商品 4需求 5新闻 6文档
     * @param link 路由地址
     * @return bool
     */
    public function searchApi(Request $request) {
        try{
            $params = [
                'content' => trim($request->get('keyword',1)),
                'count' => 1,
                'type' => intval($request->get('type',1)),
                'status' => 1,
            ];
            if(empty($params['content'])){
                return $this->writeJson(1,null,'请填写关键词！');
            }
            $res = HotSearchs::insertSearch($params);

            if($res){
                return $this->writeJson(0,null);
            }else{
                return $this->writeJson(1,null,'系统错误！');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }


}